import axios from 'axios'
import { Message } from 'element-ui'
import store from '@/store'
// const service = axios.create({
//   baseURL: process.env.VUE_APP_BASE_API
// })
// service.interceptors.request.use()
// service.interceptors.request.use()

const request = axios.create({
  timeout: 5000,
  baseURL: '/api'
})

request.interceptors.request.use(config => {
  if (store.state.user.token) {
    config.headers.Authorization = store.state.user.token
  }
  return config
}, error => {
  return Promise.reject(error)
})

request.interceptors.response.use(response => {
  // console.log(response)
  const { data, status, message } = response.data
  // console.log(data, status, message)
  if (status === 0) {
    return data
  } else {
    Message.error(message)
    return Promise.reject(new Error(message))
  }
}, error => {
  Message.error(error.message)
  return Promise.reject(error)
})

export default request
